// pages/user/registerLeader/index.js
import requestUrls from '../../../requestUrl.js'
import requestUtil from '../../../baseRequest.js'
import { $Message } from '../../../components/base/index.js'

let openid = wx.getStorageSync('openid')

Page({

    /**
     * 页面的初始数据
     */
    data: {
        openid: openid,
        uploadBusinessLicense: {
            count: 1,
            uploadComplete: 'uploadBusinessLicenseComplete',
            attentionText: '1.营业执照仅需上传一张图片',
            uploadUrl: requestUrls.baseUrl + requestUrls.uploadUrl.busindessLicence,
            isUpload: false
        },
        uploadIdCard: {
            count: 2,
            uploadComplete: 'uploadIdCardComplete',
            attentionText: '1.身份证照片需上传正反两面',
            uploadUrl: requestUrls.baseUrl + requestUrls.uploadUrl.idCard,
            isUpload: false
        },
        uploadApplicants: {
            count: 1,
            sourceType: 'camera',
            uploadComplete: 'uploadApplicantsComplete',
            attentionText: '1.申请人照片仅需上传一张\n2.申请人照片必须实景拍照上传',
            uploadUrl: requestUrls.baseUrl + requestUrls.uploadUrl.applicant,
            isUpload: false
        },
        uploadCompanyLogo: {
            count: 1,
            uploadComplete: 'uploadCompanyLogoComplete',
            attentionText: '1.公司Logo图片仅需上传一张',
            uploadUrl: requestUrls.baseUrl + requestUrls.uploadUrl.logo,
            isUpload: false
        },
    },

    /**提交表单信息 */
    submitForm({ detail }) {
        console.log(detail.value)
        if (this.checkFormData(detail.value)) {
            this.setData({
                ['uploadBusinessLicense.isUpload']: true,
                formTemp: detail.value
            })
            wx.showLoading({
                title: '图片上传中'
            })
        }
    },
    /**检查表单信息 */
    checkFormData(data) {
        // return false
        if (!data.employeeName || data.employeeName == '') {
            this.showErrorMessage('真实姓名不能为空')
            return false
        } else if (!data.companyName) {
            this.showErrorMessage('公司名称不能为空')
            return false
        } else if (!data.companyRegion) {
            this.showErrorMessage('请选择公司所在地')
            return false
        } else if (!data.businessLicense || data.businessLicense.length != 1) {
            this.showErrorMessage('公司营业执照上传图片数量有误')
            return false
        } else if (!data.companyLogo || data.companyLogo.length != 1) {
            this.showErrorMessage('公司LOGO上传图片数量有误')
            return false
        } else if (!data.idCards || data.idCards.length != 2) {
            this.showErrorMessage('身份证照片图片数量有误')
            return false
        } else if (!data.applicants || data.applicants.length != 1) {
            this.showErrorMessage('申请人照片上传图片数量有误')
            return false
        }
        return true
    },
    /**完成营业执照上传回掉*/
    uploadBusinessLicenseComplete({ detail }) {
        console.log('完成营业执照上传回掉', detail)
        this.setData({
            ['formTemp.businessLicense']: { 'url': detail[0].url, 'id': detail[0].id },
            ['uploadBusinessLicense.isUpload']: false,
            ['uploadIdCard.isUpload']: true
        })
    },
    /**完成身份证照片上传回掉*/
    uploadIdCardComplete({ detail }) {
        console.log('完成身份证照片上传回掉', detail)
        this.setData({
            ['formTemp.idCards']: detail.map((item) => { return { 'url': item.url, 'id': item.id } }),
            ['uploadIdCard.isUpload']: false,
            ['uploadApplicants.isUpload']: true
        })
    },
    /**完成申请人照片上传回掉*/
    uploadApplicantsComplete({ detail }) {
        console.log('完成申请人照片上传回掉', detail)
        this.setData({
            ['formTemp.applicants']: { 'url': detail[0].url, 'id': detail[0].id },
            ['uploadApplicants.isUpload']: false,
            ['uploadCompanyLogo.isUpload']: true
        })
    },
    /**完成公司Logo图片上传回掉 */
    uploadCompanyLogoComplete({ detail }) {
        console.log('完成公司Logo图片上传回掉', detail)
        this.setData({
            ['formTemp.companyLogo']: { 'url': detail[0].url, 'id': detail[0].id },
            ['uploadCompanyLogo.isUpload']: false
        })
        wx.hideLoading()
        this.registerLeader()
    },
    /**请求救援公司负责人认证 */
    registerLeader() {
        console.log('formTemp', this.data.formTemp)
        let param = this.data.formTemp
        param.companyRegion = param.companyRegion.join(',')
        param.openid = this.data.openid
        console.log('param', param)
        let url = requestUrls.leader.registerLeader
        if (this.data.type === '2') {
            url = requestUrls.leader.updateLeaderInfo
        }
        requestUtil.postRequest(url, param, (res) => {
            getApp().updateGlobalUserManagerStatus('0')
            wx.showModal({
                title: '温馨提示',
                content: '救援公司负责人认证信息提交成功，请耐心等待审核',
                showCancel: false,
                success: res => {
                    if (res.confirm) wx.navigateBack()
                }
            })

            // wx.navigateBack()
        }, () => {})
    },
    /**获取救援公司负责人信息 */
    getLeaderInfo() {
        if (!this.data.openid) {
            this.setData({
                openid: wx.getStorageSync('openid')
            })
        }
        let param = {
            'openid': this.data.openid
        }
        requestUtil.postRequest(requestUrls.leader.getLeaderInfo, param, (res) => {
            console.log('负责人信息：', res)
            console.log(res)
            let info = res
            let businessLicense = []
            if (info.businessLicense) businessLicense.push(info.businessLicense)
            info.businessLicense = businessLicense
            let applicants = []
            if (info.applicants) applicants.push(info.applicants)
            info.applicants = applicants
            let companyLogo = []
            if (info.companyLogo) companyLogo.push(info.companyLogo)
            info.companyLogo = companyLogo
            info.companyRegion = info.companyRegion ? info.companyRegion.split(',') : null
                // let idCards = []
                // if (info.idCards) idCards.push(info.idCards)
            info.idCards = info.idCards || []
            this.setData({
                leaderInfo: info
            })
        }, () => {})
    },
    /**显示错误信息 */
    showErrorMessage(msg) {
        $Message({
            content: msg,
            type: 'error'
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.data.type = options.type || '1'
        this.getLeaderInfo()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    // onShareAppMessage: function () {

    // }
})